﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace NLBS.Utility
{
    public class FileHelp
    {
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public static void DeleteFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            else
            {
                throw new Exception("该文件不存在");
            }
        }

        /// <summary>
        /// 删除该目录下的所有文件
        /// </summary>
        /// <param name="path">目录路径</param>
        public static void DeleteDir(string path)
        {
            DirectoryInfo dirs=new DirectoryInfo(path);
            if (dirs.GetFiles().Length==0 && dirs.GetDirectories().Length==0)
            {
                Directory.Delete(path);
            }
            else
            {
                throw new Exception("该文件夹不可删除");
            }
        }

        /// <summary>
        /// 获取该目录下的所有目录
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns>返回该目录下的所有路径</returns>
        public static DirectoryInfo[] GetDirectory(string path)
        {
            if (Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                DirectoryInfo[] dirs = dir.GetDirectories();
                return dirs;
            }
            else
            {
                throw new Exception("该目录不存在");
            }
        }
        /// <summary>
        /// 获取该目录下的所有文件
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns>返回该目录下的所有文件</returns>
        public static FileInfo[] GetFiles(string path)
        {
            List<string> fileList = new List<string>();
            if (Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileInfo[] files = dir.GetFiles();
                return files;
            }
            else
            {
                throw new Exception("该目录不存在");
            }
        }
    }
}
